﻿using System;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Configuration;

namespace Wild2.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            Int32? userId = Models.Auth.AuthOnSite.GetUserId();
            if (userId.HasValue)
            {
                if (!Models.Auth.AuthOnSite.IsSiteAdmin(userId.Value))
                {
                    return RedirectToAction("Index", "Login");
                }
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            ViewData["datestart"] = HttpContext.Application["datestart"];
            return View();
        }

        public ActionResult Restart()
        {
            Int32? userId = Models.Auth.AuthOnSite.GetUserId();
            if (userId.HasValue)
            {
                if (!Models.Auth.AuthOnSite.IsSiteAdmin(userId.Value))
                {
                    return RedirectToAction("Index", "Login");
                }
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            HttpRuntime.UnloadAppDomain();
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult SQL()
        {
            Int32? userId = Models.Auth.AuthOnSite.GetUserId();
            if (userId.HasValue)
            {
                if (!Models.Auth.AuthOnSite.IsSiteAdmin(userId.Value))
                {
                    return RedirectToAction("Index", "Login");
                }
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            if (Request.QueryString["sql"] != null)
            {
                int type;
                int.TryParse(Request.QueryString["sql"], out type);
                if (type == 1) ViewData["sql"] = Resources.Admin.SQLTemplate1;
                if (type == 2) ViewData["sql"] = Resources.Admin.SQLTemplate2;
                if (type == 3) ViewData["sql"] = Resources.Admin.SQLTemplate3;    
            }
            else
            {
                ViewData["sql"] = @"SELECT TOP(35) * FROM 

ORDER BY ID DESC";
            }
            return View();
        }

        [HttpPost]
        public ActionResult SQL(String sql, String db)
        {
            ViewData["sql"] = sql;
            Int32? userId = Models.Auth.AuthOnSite.GetUserId();
            if (userId.HasValue)
            {
                if (Models.Auth.AuthOnSite.IsSiteAdmin(userId.Value))
                {
                    String result = "<table class=\"sample\">";
                    bool showname = true;
                    int z = 1;
                    try
                    {
                        String dbase = "MyConnect";
                        if (db == "statistic") dbase = "MyConnectStatistic";
                        using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings[dbase].ConnectionString))
                        {
                            using (var cmd = new SqlCommand(sql, conn))
                            {
                                cmd.CommandTimeout = 120;
                                cmd.Connection.Open();
                                using (SqlDataReader reader = cmd.ExecuteReader())
                                {
                                    while (reader.Read())
                                    {
                                        if (showname)
                                        {
                                            result += "<tr>";
                                            result += "<td>" + HttpUtility.HtmlEncode("#") + "</td>";
                                            for (int i = 0; i < reader.FieldCount; i++)
                                            {
                                                result += "<th>";
                                                result += HttpUtility.HtmlEncode(reader.GetName(i));
                                                result += "</th>";
                                            }
                                            result += "</tr>";
                                        }
                                        result += "<tr>";
                                        result += "<td>" + z.ToString(CultureInfo.InvariantCulture) + "</td>";
                                        for (int i = 0; i < reader.FieldCount; i++)
                                        {
                                            result += "<td>";
                                            if (reader[i] != DBNull.Value) { result += HttpUtility.HtmlEncode(reader[i].ToString()); }
                                            else { result += "<i>NULL</i>"; }
                                            result += "</td>";
                                        }
                                        result += "</tr>";
                                        showname = false;
                                        if (z >= 150) break;
                                        z++;
                                    }
                                }
                                cmd.Connection.Close();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        ViewData["error"] = e.Message;
                    }
                    result += "</table>";
                    ViewData["ResultData"] = result;
                }
                else
                {
                    return RedirectToAction("Index", "Login");
                }
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            return View();
        }
    }
}
